﻿DailyTracker = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceEvent-2.0")
DailyTracker.DebugEnabled = false;
DailyTracker.VersionNo = "v0.1";

function DailyTracker:RegisterChatCommands()
	local dailyTrackerCommands = {
		type = 'group',
		args = {
			show = {
				type = 'execute',
				name = DailyTrackerStrings.ShowCommand,
				desc = DailyTrackerStrings.ShowCommandDescription,
				func = function()
					DailyTrackerFrame:Show();
				end
			},
			debug = {
				type = 'toggle',
				name = DailyTrackerStrings.DebugCommand,
				desc = DailyTrackerStrings.DebugCommandDescription,
				get = function()
					return DailyTracker.DebugEnabled;
				end,
				set = function()
					DailyTracker.DebugEnabled = not DailyTracker.DebugEnabled ;
				end
			}
		}
	}
	self:RegisterChatCommand("/DailyQuestTracker", "/dqt", dailyTrackerCommands);
end

function DailyTracker:Debug(...)
	if self.DebugEnabled == true then
		self:Print(...);
	end
end

function DailyTracker:OnInitialize()
    -- Called when the addon is loaded
end

function DailyTracker:PrintCurrentDailies()
	local categories, quests = self:GetQuestDb();
	for i=1,table.getn(categories) do
		local category = categories[i];
		local currentQuest = self:GetCurrentQuest( category.Id );
		if currentQuest then
			self:Print( category.CategoryName .. ':', self:QuestToString( currentQuest ) );
		end
	end			
end

function DailyTracker:FoundQuest(questTitle, silent)
	-- is it a random daily?
	local quest = self:GetQuestByName( questTitle );
	if quest then
		self:Debug( "Found quest", quest.Title, quest.Location );		
		if not silent == true then
			-- send via AceComm
			DailyTrackerComm:SendDailyResponse( quest.Category, quest.Id );
		end		
		
		-- set our current quest
		self:SetCurrentQuest( quest.Category, quest.Id );
		self:RefreshFrame();
	else
		self:Debug( "could not find " .. questTitle );
	end
end

function DailyTracker:ProcessGossipQuest(questTitle, somethingIDontCareAbout, somethingElseIDontCareAbout, ...)
	if questTitle then
		self:FoundQuest( questTitle );
		self:ProcessGossipQuest( ... );
	end
end

function DailyTracker:GOSSIP_SHOW()
	self:ProcessGossipQuest( GetGossipAvailableQuests() );
end

function DailyTracker:QUEST_DETAIL()
	self:FoundQuest( GetTitleText() );
end

function DailyTracker:ReceiveDailyQuest(prefix, sender, distribution, questCategoryId, questId)
	self:Debug("Received daily information from " .. distribution .. " (" .. sender .. "): ", questCategoryId, questId);
	if questCategoryId and questId then
		quest = DailyTracker:GetQuest( questCategoryId, questId );
		if quest then
			self:FoundQuest( quest.Title, true );
		else
			self:Print( "COULD NOT FIND QUEST: ", questCategoryId, questId );
		end
	end
end

function DailyTracker:OnEnable()
	-- Called when the addon is enabled
	self:RegisterEvent("GOSSIP_SHOW");
	self:RegisterEvent("QUEST_DETAIL");
	self:RegisterChatCommands();
	self:Print("Successfully loaded.");
end

function DailyTracker:OnDisable()
    -- Called when the addon is disabled
end
